﻿using System;
using System.Windows;
using System.Windows.Controls;
using Graphe.Collations;

namespace Graphe.Windows.Controls.Collations
{
    public class ManuscriptListControl : StackPanel
    {
        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public ManuscriptListControl()
        {
            Loaded += new RoutedEventHandler(ManuscriptListControl_Loaded);
        }

        #endregion
        #region Public Properties

        public ManuscriptCollection Manuscripts { get; set; }
        public Manuscript SelectedManuscript { get; set; }

        #endregion

        private void ManuscriptListControl_Loaded(object sender, RoutedEventArgs e)
        {
            Style = CollationsControlStylePicker.FindProperVersionResource("ManuscriptsControlPanelStyle") as Style;
        }

        public void Render()
        {
            Children.Clear();
            for (int i = 0; i <= Manuscripts.Count - 1; i++)
            {
                if (i > 0)
                {
                    TextBlock commaBlock = new TextBlock();
                    commaBlock.Text = ", ";
                    commaBlock.Style = CollationsControlStylePicker.FindProperVersionResource("RegularUnicodeCollationText") as Style;
                    Children.Add(commaBlock);
                }

                Graphe.Collations.Manuscript manuscript = Manuscripts[i];

                CollationTextBlock textBlock = new CollationTextBlock();
                textBlock.Style = CollationsControlStylePicker.FindProperVersionResource("RegularUnicodeCollationText") as Style;
                textBlock.Text += manuscript.Symbol;
                textBlock.Tag = manuscript;
                textBlock.MouseDown += new System.Windows.Input.MouseButtonEventHandler(textBlock_MouseDown);
                Children.Add(textBlock);
            }
        }

        private void textBlock_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            CollationTextBlock textBlock = sender as CollationTextBlock;

            SelectedManuscript = textBlock.Tag as Manuscript;
        }


    }
}
